/*
 * MAX7456 osd low level functions library
 *
 * Written by Florian MAZEN and Pierre COL, 2008-2009
 * <euronym AT free.fr>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#include <SPI.h>
#include "max7456.h"
#include "pinout.h"

/* Low level register read */
unsigned char max7456_read_register(unsigned char adress) 
{
	char val;
	max7456_cs_enable();
	SPI.transfer(adress);
	val = SPI.transfer(0);
	max7456_cs_disable();
	return val;
}

/* Low level register read */
void max7456_write_register(unsigned char adress, unsigned char value)
{
	max7456_cs_enable();
	SPI.transfer(adress);
	SPI.transfer(value);
	max7456_cs_disable();
}

/* Write a signle character in auto-increment mode */
inline void max7456_write_autoincrement_byte(unsigned char value)
{
	max7456_cs_enable();
	SPI.transfer(value);
	max7456_cs_disable();
}


/* This two functions are required by the library to set the Chip select pin for the max7456 */
inline void max7456_cs_enable(void)
{
  digitalWrite(MAX7456SELECT,LOW);
}

inline void max7456_cs_disable(void)
{
  digitalWrite(MAX7456SELECT,HIGH);
}

/* ASCII to OSD codes conversion table */
PROGMEM unsigned char ascii2osdChars[128] = {
	//	OSD	   ASCII
		0xff,	// 00
		0xcb,	// 01
		0xcb,	// 02
		0xcb,	// 03
		0xcb,	// 04
		0xcb,	// 05
		0xcb,	// 06
		0xcb,	// 07
		0xcb,	// 08
		0xcb,	// 09
		0xcb,	// 0a
		0xcb,	// 0b
		0xcb,	// 0c
		0xcb,	// 0d
		0xcb,	// 0e
		0xcb,	// 0f
		0xcb,	// 10
		0xcb,	// 11
		0xcb,	// 12
		0xcb,	// 13
		0xcb,	// 14
		0xcb,	// 15
		0xcb,	// 16
		0xcb,	// 17
		0xcb,	// 18
		0xcb,	// 19
		0xcb,	// 1a
		0xcb,	// 1b
		0xcb,	// 1c
		0xcb,	// 1d
		0xcb,	// 1e
		0xcb,	// 1f
		0x00,	// 20
		0xcb,	// 21
		0x48,	// 22
		0xcb,	// 23
		0xcb,	// 24
		0xcb,	// 25
		0xcb,	// 26
		0x46,	// 27
		0x3f,	// 28
		0x40,	// 29
		0xcb,	// 2a
		0xcb,	// 2b
		0x45,	// 2c
		0x49,	// 2d
		0x41,	// 2e
		0x47,	// 2f
		0x0a,	// 30
		0x01,	// 31
		0x02,	// 32
		0x03,	// 33
		0x04,	// 34
		0x05,	// 35
		0x06,	// 36
		0x07,	// 37
		0x08,	// 38
		0x09,	// 39
		0x44,	// 3a
		0x43,	// 3b
		0x4a,	// 3c
		0xcb,	// 3d
		0x4b,	// 3e
		0x42,	// 3f
		0x4c,	// 40
		0x0b,	// 41
		0x0c,	// 42
		0x0d,	// 43
		0x0e,	// 44
		0x0f,	// 45
		0x10,	// 46
		0x11,	// 47
		0x12,	// 48
		0x13,	// 49
		0x14,	// 4a
		0x15,	// 4b
		0x16,	// 4c
		0x17,	// 4d
		0x18,	// 4e
		0x19,	// 4f
		0x1a,	// 50
		0x1b,	// 51
		0x1c,	// 52
		0x1d,	// 53
		0x1e,	// 54
		0x1f,	// 55
		0x20,	// 56
		0x21,	// 57
		0x22,	// 58
		0x23,	// 59
		0x24,	// 5a
		0xcb,	// 5b
		0xcb,	// 5c
		0xcb,	// 5d
		0xcb,	// 5e
		0xcb,	// 5f
		0x46,	// 60
		0x25,	// 61
		0x26,	// 62
		0x27,	// 63
		0x28,	// 64
		0x29,	// 65
		0x2a,	// 66
		0x2b,	// 67
		0x2c,	// 68
		0x2d,	// 69
		0x2e,	// 6a
		0x2f,	// 6b
		0x30,	// 0c
		0x31,	// 6d
		0x32,	// 6e
		0x33,	// 6f
		0x34,	// 70
		0x35,	// 71
		0x36,	// 72
		0x37,	// 73
		0x38,	// 74
		0x39,	// 75
		0x3a,	// 76
		0x3b,	// 77
		0x3c,	// 78
		0x3d,	// 79
		0x3e,	// 7a
		0xcb,	// 7b
		0xcb,	// 7c
		0xcb,	// 7d
		0xcb,	// 7e
		0xcb};	// 7f

/* Convert ASCII code to OSD default table code for characters <= 127*/
unsigned char max7456_ascii2osdChars(unsigned char asciiCode) 
{
	if (asciiCode > 127)
		return asciiCode;
	else
		return ascii2osdChars[asciiCode];
}
